package cn.psh.org.controller;


import cn.psh.basic.util.PageList;
import cn.psh.basic.util.AjaxResult;
import cn.psh.basic.util.annotation.LogAnno;
import cn.psh.basic.util.annotation.PreAuthorize;
import cn.psh.org.domain.Department;
import cn.psh.org.query.DepartmentQuery;
import cn.psh.org.service.impl.DepartmentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dept")
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentServiceImpl departmentService;

    @GetMapping("/{id}")
    @LogAnno(operateType = "查询一个数据",operateor="feifei",lid="001")
    @PreAuthorize(name = "查询单个部门数据", sn = "dept:getOne")
    public Department queryById(@PathVariable Long id) {
        return departmentService.queryById(id);
    }

    /*
    查询所有
     */
    @GetMapping
    @LogAnno(operateType = "查询全部",operateor="feifei",lid="001")
    @PreAuthorize(name = "部门列表", sn = "dept:getAll")
    public List<Department> queryAll() {
        return departmentService.queryAll();
    }

    /**
     * 添加或修改数据
     */
    @PutMapping
    @LogAnno(operateType = "添加或修改数据",operateor="feifei",lid="001")
    @PreAuthorize(name = "保存列表", sn = "dept:save")
    public AjaxResult addAndUpdate(@RequestBody Department department) {

        try {
            if (null == department.getId()) {
                // 添加
                departmentService.save(department);
            } else {
                // 修改
                departmentService.update(department);
            }
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.OJBK().setMessage("系统错误，正在殴打程序员！！骚等");
    }

    /**
     * 根据id删除一个数据
     */
    @DeleteMapping("/{id}")
    @LogAnno(operateType = "删除一个数据",operateor="feifei",lid="001")
    @PreAuthorize(name = "删除部门", sn = "dept:delete")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            departmentService.del(id);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.OJBK().setMessage("系统错误，正在殴打程序员！！骚等");
    }

    @PostMapping
    @LogAnno(operateType = "分页和高级查询和查询全部",operateor="feifei",lid="001")
    @PreAuthorize(name = "部门分页", sn = "dept:list")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        return departmentService.queryData(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    @LogAnno(operateType = "批量删除",operateor="feifei",lid="001")
    @PreAuthorize(name = "部门批量删除", sn = "dept:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.OJBK();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统错误！！请骚后重试！");
        }

    }
    /**
     * 无限极树
     */
    @GetMapping("/tree")
    @PreAuthorize(name = "部门树", sn = "dept:tree")
    public List<Department> findDeptTree(){
        return departmentService.queryTree();
    }

}

